import 'dart:ui';

import 'package:dio/dio.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/services.dart';
import 'package:flutter_juwo/common/common_index.dart';
import 'package:flutter/material.dart';
import 'package:flutter_juwo/ui/custom/LoadingDialog.dart';
import 'package:flutter_juwo/ui/custom/real_rich_text.dart';
import 'package:flutter_juwo/ui/model/base_entity.dart';
import 'package:flutter_juwo/ui/model/sim_code_entity.dart';
import 'package:flutter_juwo/ui/utils/toast_utils.dart';
import 'package:flutter_juwo/ui/utils/utils.dart';

///************************
/// @auther nb
/// @function 输入邀请码
/// @创建日期 2019-10-28 14:23:28
///************************
class InputInvitationCode extends StatefulWidget {
  @override
  _InputInvitationCodeState createState() => _InputInvitationCodeState();
}

class _InputInvitationCodeState extends State<InputInvitationCode> {
  int line1Color = 0xFFe5e5e5;

  FocusNode _nameFocusNode;
  TextEditingController _nameController = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _nameFocusNode = FocusNode();
    _nameFocusNode.addListener(() {
      if (_nameFocusNode.hasFocus) {
        setState(() {
          line1Color = 0xFFdf3033;
        });
      } else {
        setState(() {
          line1Color = 0xFFe5e5e5;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Column(
        children: <Widget>[
          ///x
          getRow1(),

          ///登录
          getRow2(),

          ///输入手机号
          getRow4(),
          getLine1(),

          getButton(),
          getBottom(),
        ],
      ),
    );
  }

  getRow1() {
    return Container(
      alignment: Alignment.centerLeft,
      margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(30),
          MediaQueryData.fromWindow(window).padding.top, 0, 0),
      height: ScreenUtil().setWidth(88),
      child: GestureDetector(
        onTap: () {
          Application.router.pop(context);
        },
        child: Image.asset(
          Utils.getImgPath("icon_delete"),
          width: ScreenUtil().setWidth(32),
          height: ScreenUtil().setWidth(32),
        ),
      ),
    );
  }

  getRow2() {
    return Container(
      margin: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(92), ScreenUtil().setWidth(88), 0, 0),
      alignment: Alignment.centerLeft,
      child: Text(
        "输入邀请码",
        style: TextStyle(
          fontSize: ScreenUtil().setWidth(48),
          color: Colours.appC1,
          fontFamily: Font.PingFangBlod,
        ),
        /*TextStyles.custom(48, Colours.appC1, AppFontFamily.PingFangBlod)*/
      ),
    );
  }

  getRow3() {
    return Container(
      margin: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(90), ScreenUtil().setWidth(30), 0, 0),
      alignment: Alignment.centerLeft,
      child: RealRichText([
        TextSpan(
            text: "还没有账号，",
            style: TextStyles.custom(
                24, Colours.appC1, Font.PingFangMedium)),
        TextSpan(
            text: "立即注册",
            style: TextStyle(
              color: Color(0xFFE61F1C),
              fontSize: ScreenUtil().setSp(24),
              fontFamily: Font.PingFangMedium,
              decorationStyle: TextDecorationStyle.solid,
              decoration: TextDecoration.underline,
              decorationColor: Color(0xFFE61F1C),
            ),
            recognizer: TapGestureRecognizer()
              ..onTap = () {
                ///跳转到 注册
              })
      ]),
    );
  }

  getRow4() {
    return Container(
      margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(90),
          ScreenUtil().setWidth(90), ScreenUtil().setWidth(90), 0),
      child: TextField(
        style:TextStyles.textFiled,
        controller: _nameController,
        focusNode: _nameFocusNode,
        decoration: InputDecoration(
          hintText: "请输入邀请码",
          hintStyle: TextStyles.custom(
              28, Color(0xFF999999), Font.PingFangMedium),
          border: InputBorder.none,
        ),
      ),
    );
  }

  getLine1() {
    return Container(
      margin: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(90), 0, ScreenUtil().setWidth(90), 0),
      width: ScreenUtil().setWidth(750),
      color: Color(line1Color),
      height: ScreenUtil().setWidth(1),
    );
  }

  getButton() {
    return Container(
      margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(56),
          ScreenUtil().setWidth(96), ScreenUtil().setWidth(56), 0),
      child: UiUtils.bottomBotton("确认", () {
        String code = _nameController.text;
        if (code.isEmpty) {
          ToastUtil.showShortErrToast("邀请码不能为空");
          return;
        }
        showDialog(
            context: context,
            barrierDismissible: false,
            builder: (BuildContext context) {
              return new LoadingDialog(
                text: "正在发送验证码…",
              );
            });

        _setinvitecode();
      }),
    );
  }

  getBottom() {
    return Container(
      alignment: Alignment.centerRight,
      margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(90),
          ScreenUtil().setWidth(30), ScreenUtil().setWidth(90), 0),
      child: GestureDetector(
        onTap: () {
          Navigator.pop(context); //退出此页面
          Navigator.pop(context); //退出输入验证码页面
          Navigator.pop(context); //退出输入手机号页面
        },
        child: Container(
          alignment: Alignment.center,
          child: Text(
            "无邀请码，跳过",
            style: TextStyles.custom(
                24, Color(0xFF999999), Font.PingFangMedium),
          ),
        ),
      ),
    );
  }

  Future _setinvitecode() async {
    //17099298919  bin110123
    FormData params = FormData.from({"inviteCode": _nameController.text});
    DioManager.getInstance().post(Api.SETINVITECODE, params,
        //正常回调
        (data) {
      print("输入邀请码:$data");

      BaseEntity entity = BaseEntity.fromJson(data);
      print(entity.code == Api.SUCCESS_CODE);

      if (entity.code == Api.SUCCESS_CODE) {
        ToastUtil.showShortToast("设置成功");
        Navigator.pop(context); //退出dialog
        Navigator.pop(context); //退出此页面
        Navigator.pop(context); //退出输入验证码页面
        Navigator.pop(context); //退出输入手机号页面
        return;
      }

      if (entity.code == Api.ERROR_CODE || entity.code == Api.ERROR_CODE_0) {
        Navigator.pop(context);
        ToastUtil.showShortErrToast(entity.message);
        return;
      }
      Navigator.pop(context);
      ToastUtil.showShortErrToast(Api.ERR_NOTHING);
    },
        //错误回调
        (error) {
      ToastUtil.showShortErrToast(Api.ERR_TIME_OUT);
      Navigator.pop(context);
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _nameController.dispose();
  }
}
